<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        if($_SESSION['login'] != true) {
            $this->display();
        }
        else{
            redirect(U('System/listPage'));
        }
    }

    public function adminLogin(){

        session_start();
        if($_SESSION['login'] != true) {
            $adminName = I('post.adminName');
            $adminPassword = I('post.adminPassword');
            $this->getDataFromAdmin($adminName, $adminPassword);
        }
        else{
            session_destroy();
            $adminName = I('post.adminName');
            $adminPassword = I('post.adminPassword');
            $this->getDataFromAdmin($adminName, $adminPassword);
        }
    }

    public function getDataFromAdmin($adminName,$adminPassword){
        $User = M('admin');
        $condition['name'] = $adminName;
        $condition['password'] = md5($adminPassword);
        $result = $User->where($condition)->find();
        if($adminName!='admin'){
            $this->error('该用户无管理员权限');
        }else if(!$result){
            $this->error('密码错误或用户名错误');
        }
        else{
            $_SESSION['login']=true;
            $_SESSION['id'] = $result['id'];
            redirect(U('System/listPage'));
        }
    }

    public function adminLogout(){
        session_destroy();
        unset($_session);
        $this->display('index');
    }

    public function toHome(){
        redirect('http://'.$_SERVER['HTTP_HOST']);
    }
}